//
//  SDSectionView.m
//  SuperDoctor
//
//  Created by 李忠 on 7/5/16.
//  Copyright © 2016 SD. All rights reserved.
//

#import "SDSelectedSectionView.h"
#import "SDverticalButton.h"

#define padding 3

@interface SDSelectedSectionView ()

@property (nonatomic, strong) NSArray *btnTop;
@property (nonatomic, strong) NSArray *btnBottom;

@end

@implementation SDSelectedSectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI{
    
    
    SDverticalButton *tumour = [self newButtonWithTitle:@"肿瘤" withImage:@"zl"];
    SDverticalButton *angiocardiopathy = [self newButtonWithTitle:@"心血管" withImage:@"xx"];
    SDverticalButton *nerve = [self newButtonWithTitle:@"神经科" withImage:@"sj"];
    SDverticalButton *blood = [self newButtonWithTitle:@"血液科" withImage:@"xy"];
    SDverticalButton *orthopedics = [self newButtonWithTitle:@"骨科" withImage:@"gt"];
    SDverticalButton *commonweal = [self newButtonWithTitle:@"公益" withImage:@"gy"];
    //MARK: - 第一行约束
    [tumour mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(padding);
        make.left.mas_offset(padding);
        make.size.mas_equalTo(tumour);
        
    }];
    [self preLayout:angiocardiopathy withBase:tumour];
    [self lastLayout:nerve withBase:tumour whithPre:angiocardiopathy];
    
    //MARK: - 下一行约束
    [blood mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_offset(padding);
        make.top.mas_equalTo(tumour.mas_bottom).mas_offset(padding);
        make.bottom.mas_equalTo(self.mas_bottom).mas_offset(-padding);
        make.size.mas_equalTo(tumour);
        
    }];
    
    [self preLayout:orthopedics withBase:blood];
    [self lastLayout:commonweal withBase:blood whithPre:orthopedics];
   
}

- (void)preLayout:(SDverticalButton *)btn withBase:(SDverticalButton *)baseBtn{
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(baseBtn.mas_trailing).mas_offset(padding);
        make.size.mas_equalTo(baseBtn);
        make.centerY.mas_equalTo(baseBtn.mas_centerY);
    }];

}
- (void)lastLayout:(SDverticalButton *)btn withBase:(SDverticalButton *) baseBtn whithPre:(SDverticalButton *)preBtn{
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-padding);
        make.leading.mas_equalTo(preBtn.mas_trailing).mas_offset(padding);
        make.centerY.mas_equalTo(baseBtn.mas_centerY);
        make.size.mas_equalTo(baseBtn);
        
    }];
}
- (SDverticalButton *)newButtonWithTitle:(NSString *)title withImage:(NSString *)imageStr{
    
    SDverticalButton *btn = [[SDverticalButton alloc] initWithFrame:CGRectZero];
    
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:imageStr] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor whiteColor];
    [self addSubview:btn];
    return btn;
}

- (void)clickButton:(UIButton *)sender{
    if (_pushVCBlock != nil) {
     
        NSInteger index = [self.subviews indexOfObject:sender];
        NSString *titleName = sender.currentTitle;
        _pushVCBlock(index, titleName);
    }
}

@end
